<?php

// optional output=screen will return results to text area
// Error if we don't have a Project ID and Crudset ID
//Incluce DB connect 
include_once "quickcrud_connect.php";
include_once "quickcrud_common.php";
//include_once "commonctls.php";
include_once "CF_mysql_com.php";
include_once "CF_all_com.php";


if (!isset($_REQUEST['project_id'])){
	print 'Please Select a project and display set (crudset)';
    exit;
}

$crudset_id = $_REQUEST['crudset_id'];
$project_id = $_REQUEST['project_id'];

$menu_html = make_lmenu($project_id);
//print $menu_html;


if($_REQUEST['output'] == 'screen'){
// Displays to textarea on screen
echo "<html>\n<head>\n</head>\n<body>";
echo "<TEXTAREA NAME=\"comments\" COLS='80' ROWS='20'>$menu_html</TEXTAREA>\n";
echo "</body></html>";

}
else{
$project_path = lookup_value('project_path', 'projects', "project_id = $project_id");

$subdirectory = lookup_value('subdirectory', 'crudsets', "crudset_id = $crudset_id");

// if subdirectory path not exists, create
if(!empty($subdirectory)){
	$subdirname = rtrim($subdirectory, "/");
		if(!file_exists('projects/' . $project_path . '/tplcommon'))
		{
			mkdir('projects/' . $project_path . '/tplcommon');
		}
	}


$output_file = PROJECT_SUBPATH . $project_path . '/tplcommon/leftmenu.php';
//print $output_file;
$result = "Wrote standard left menu To $output_file";
$result = urlencode($result);
file_put_contents($output_file,$menu_html);
       // $header_text = "Location: leftmenu.php?project_id=$project_id&crudset_id=$crudset_id&result=$result";
       // header($header_text); /* Redirect browser */
	   
	   print $result;
	   
        exit;
 }



// ----------------------------------------------------------------------------------------------------
function make_lmenu($project_id){
	$qry_get_columns = "SELECT * FROM crudsets WHERE project_id = $project_id ";
	$list_array = get_mysql_array($qry_get_columns);
	$menu_html = "<?php \n\n?>\n";
	foreach ($list_array as $rowkey => $currentrow){
		$crudset_id = $currentrow['crudset_id'];

		$crudset_name = $currentrow['cs_name'];

		$menu_html .= "\t<a href='$crudset_name" . '_list.php' . "' title='$crudset_name'>$crudset_name</a><br />\n";

		}		
	return $menu_html;
}